import 'package:flutter/material.dart';
import 'package:happy_around_us/RongCloud/RCConfig.dart';
import 'package:happy_around_us/RongCloud/RCIMPage.dart';
import 'package:happy_around_us/RongCloud/RCCallPage.dart';
import 'package:rongcloud_im_wrapper_plugin/rongcloud_im_wrapper_plugin.dart';

class RCServerInitPage extends StatefulWidget {
  RCServerInitPage();

  @override
  State<RCServerInitPage> createState() => _RCServerInitPageState();
}

class _RCServerInitPageState extends State<RCServerInitPage> {
  int loginIndex = RCConfig.loginIndex;
  var state = RCIMIWConnectionStatus.signOut;

  @override
  void initState() {
    super.initState();

    RCConfig.requestRCPermission();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('flutter call plugin'),
        ),
        body: Center(
          child: Column(
            children: [
              Divider(
                height: 50,
                color: Colors.transparent,
              ),
              //添加一个输入框，用于输入 appkey
              TextField(
                decoration: InputDecoration(
                  hintText: 'AppKey：${RCConfig.appKey}',
                ),
              ),
              Divider(
                height: 50,
                color: Colors.transparent,
              ),
              OutlinedButton(
                child: Text(
                  "切换用户 ${loginIndex}\ntoken = ${RCConfig
                      .LoginTokens[loginIndex]}",
                  textAlign: TextAlign.center,
                ),
                onPressed: () {
                  RCConfig.loginIndex = (RCConfig.loginIndex + 1) % 3;
                  setState(() => loginIndex = RCConfig.loginIndex);
                },
              ),
              Divider(
                height: 50,
                color: Colors.transparent,
              ),
              //添加一个按钮，用于连接融云服务器
              TextButton(
                onPressed: () => _connect(context),
                child: Text(
                  '连接融云服务器\n' + state.toString(),
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Colors.green,
                    fontSize: 15,
                    decoration: TextDecoration.none,
                  ),
                ),
              ),
              if (state == RCIMIWConnectionStatus.connected)
                Row(
                  children: [
                    TextButton(
                      onPressed: () => nextPage(false),
                      child: Text(
                        'IMPage',
                        style: TextStyle(
                          color: Colors.green,
                          fontSize: 15,
                          decoration: TextDecoration.none,
                        ),
                      ),
                    ),
                    TextButton(
                      onPressed: () => nextPage(true),
                      child: Text(
                        'CallPage',
                        style: TextStyle(
                          color: Colors.green,
                          fontSize: 15,
                          decoration: TextDecoration.none,
                        ),
                      ),
                    ),
                  ],
                ),
            ],
          ),
        ),
      ),
    );
  }

  // 连接融云服务器
  void _connect(BuildContext context) async {
    // 获取输入框的值
    // 调用 connect 方法，连接融云服务器
    // 跳转到 CallPage 页面
    if (RCConfig.imEngine == null) {
      RCConfig.initRongCloud();
    }
    RCConfig.IMConnect(
      RCConfig.LoginTokens[loginIndex],
      refresh: () {
        setState(() => state = RCConfig.imStatus);
      },
    );
  }

  void nextPage(bool isCall) {
    FocusScope.of(context).requestFocus(new FocusNode());
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => isCall ? RCCallPage() : RCIMPage(false),
      ),
    );
  }


}
